  <html>
    <head>
      <meta charset="utf-8">
      <title>Spark StreamingCreate Repository</title>
      <style>
        #wrapper {width: 960px; margin: 0 auto;}
        /* Asciidoctor default stylesheet | MIT License | http://asciidoctor.org */
/* Uncomment @import statement below to use as custom stylesheet */
/*@import "https://fonts.googleapis.com/css?family=Open+Sans:300,300italic,400,400italic,600,600italic%7CNoto+Serif:400,400italic,700,700italic%7CDroid+Sans+Mono:400,700";*/
article,aside,details,figcaption,figure,footer,header,hgroup,main,nav,section,summary{display:block}
audio,canvas,video{display:inline-block}
audio:not([controls]){display:none;height:0}
script{display:none!important}
html{font-family:sans-serif;-ms-text-size-adjust:100%;-webkit-text-size-adjust:100%}
a{background:transparent}
a:focus{outline:thin dotted}
a:active,a:hover{outline:0}
h1{font-size:2em;margin:.67em 0}
abbr[title]{border-bottom:1px dotted}
b,strong{font-weight:bold}
dfn{font-style:italic}
hr{-moz-box-sizing:content-box;box-sizing:content-box;height:0}
mark{background:#ff0;color:#000}
code,kbd,pre,samp{font-family:monospace;font-size:1em}
pre{white-space:pre-wrap}
q{quotes:"\201C" "\201D" "\2018" "\2019"}
small{font-size:80%}
sub,sup{font-size:75%;line-height:0;position:relative;vertical-align:baseline}
sup{top:-.5em}
sub{bottom:-.25em}
img{border:0}
svg:not(:root){overflow:hidden}
figure{margin:0}
fieldset{border:1px solid silver;margin:0 2px;padding:.35em .625em .75em}
legend{border:0;padding:0}
button,input,select,textarea{font-family:inherit;font-size:100%;margin:0}
button,input{line-height:normal}
button,select{text-transform:none}
button,html input[type="button"],input[type="reset"],input[type="submit"]{-webkit-appearance:button;cursor:pointer}
button[disabled],html input[disabled]{cursor:default}
input[type="checkbox"],input[type="radio"]{box-sizing:border-box;padding:0}
button::-moz-focus-inner,input::-moz-focus-inner{border:0;padding:0}
textarea{overflow:auto;vertical-align:top}
table{border-collapse:collapse;border-spacing:0}
*,*::before,*::after{-moz-box-sizing:border-box;-webkit-box-sizing:border-box;box-sizing:border-box}
html,body{font-size:100%}
body{background:#fff;color:rgba(0,0,0,.8);padding:0;margin:0;font-family:"Noto Serif","DejaVu Serif",serif;font-weight:400;font-style:normal;line-height:1;position:relative;cursor:auto;tab-size:4;-moz-osx-font-smoothing:grayscale;-webkit-font-smoothing:antialiased}
a:hover{cursor:pointer}
img,object,embed{max-width:100%;height:auto}
object,embed{height:100%}
img{-ms-interpolation-mode:bicubic}
.left{float:left!important}
.right{float:right!important}
.text-left{text-align:left!important}
.text-right{text-align:right!important}
.text-center{text-align:center!important}
.text-justify{text-align:justify!important}
.hide{display:none}
img,object,svg{display:inline-block;vertical-align:middle}
textarea{height:auto;min-height:50px}
select{width:100%}
.center{margin-left:auto;margin-right:auto}
.stretch{width:100%}
.subheader,.admonitionblock td.content>.title,.audioblock>.title,.exampleblock>.title,.imageblock>.title,.listingblock>.title,.literalblock>.title,.stemblock>.title,.openblock>.title,.paragraph>.title,.quoteblock>.title,table.tableblock>.title,.verseblock>.title,.videoblock>.title,.dlist>.title,.olist>.title,.ulist>.title,.qlist>.title,.hdlist>.title{line-height:1.45;color:#7a2518;font-weight:400;margin-top:0;margin-bottom:.25em}
div,dl,dt,dd,ul,ol,li,h1,h2,h3,#toctitle,.sidebarblock>.content>.title,h4,h5,h6,pre,form,p,blockquote,th,td{margin:0;padding:0;direction:ltr}
a{color:#2156a5;text-decoration:underline;line-height:inherit}
a:hover,a:focus{color:#1d4b8f}
a img{border:none}
p{font-family:inherit;font-weight:400;font-size:1em;line-height:1.6;margin-bottom:1.25em;text-rendering:optimizeLegibility}
p aside{font-size:.875em;line-height:1.35;font-style:italic}
h1,h2,h3,#toctitle,.sidebarblock>.content>.title,h4,h5,h6{font-family:"Open Sans","DejaVu Sans",sans-serif;font-weight:300;font-style:normal;color:#ba3925;text-rendering:optimizeLegibility;margin-top:1em;margin-bottom:.5em;line-height:1.0125em}
h1 small,h2 small,h3 small,#toctitle small,.sidebarblock>.content>.title small,h4 small,h5 small,h6 small{font-size:60%;color:#e99b8f;line-height:0}
h1{font-size:2.125em}
h2{font-size:1.6875em}
h3,#toctitle,.sidebarblock>.content>.title{font-size:1.375em}
h4,h5{font-size:1.125em}
h6{font-size:1em}
hr{border:solid #dddddf;border-width:1px 0 0;clear:both;margin:1.25em 0 1.1875em;height:0}
em,i{font-style:italic;line-height:inherit}
strong,b{font-weight:bold;line-height:inherit}
small{font-size:60%;line-height:inherit}
code{font-family:"Droid Sans Mono","DejaVu Sans Mono",monospace;font-weight:400;color:rgba(0,0,0,.9)}
ul,ol,dl{font-size:1em;line-height:1.6;margin-bottom:1.25em;list-style-position:outside;font-family:inherit}
ul,ol{margin-left:1.5em}
ul li ul,ul li ol{margin-left:1.25em;margin-bottom:0;font-size:1em}
ul.square li ul,ul.circle li ul,ul.disc li ul{list-style:inherit}
ul.square{list-style-type:square}
ul.circle{list-style-type:circle}
ul.disc{list-style-type:disc}
ol li ul,ol li ol{margin-left:1.25em;margin-bottom:0}
dl dt{margin-bottom:.3125em;font-weight:bold}
dl dd{margin-bottom:1.25em}
abbr,acronym{text-transform:uppercase;font-size:90%;color:rgba(0,0,0,.8);border-bottom:1px dotted #ddd;cursor:help}
abbr{text-transform:none}
blockquote{margin:0 0 1.25em;padding:.5625em 1.25em 0 1.1875em;border-left:1px solid #ddd}
blockquote cite{display:block;font-size:.9375em;color:rgba(0,0,0,.6)}
blockquote cite::before{content:"\2014 \0020"}
blockquote cite a,blockquote cite a:visited{color:rgba(0,0,0,.6)}
blockquote,blockquote p{line-height:1.6;color:rgba(0,0,0,.85)}
@media screen and (min-width:768px){h1,h2,h3,#toctitle,.sidebarblock>.content>.title,h4,h5,h6{line-height:1.2}
h1{font-size:2.75em}
h2{font-size:2.3125em}
h3,#toctitle,.sidebarblock>.content>.title{font-size:1.6875em}
h4{font-size:1.4375em}}
table{background:#fff;margin-bottom:1.25em;border:solid 1px #dedede}
table thead,table tfoot{background:#f7f8f7}
table thead tr th,table thead tr td,table tfoot tr th,table tfoot tr td{padding:.5em .625em .625em;font-size:inherit;color:rgba(0,0,0,.8);text-align:left}
table tr th,table tr td{padding:.5625em .625em;font-size:inherit;color:rgba(0,0,0,.8)}
table tr.even,table tr.alt,table tr:nth-of-type(even){background:#f8f8f7}
table thead tr th,table tfoot tr th,table tbody tr td,table tr td,table tfoot tr td{display:table-cell;line-height:1.6}
h1,h2,h3,#toctitle,.sidebarblock>.content>.title,h4,h5,h6{line-height:1.2;word-spacing:-.05em}
h1 strong,h2 strong,h3 strong,#toctitle strong,.sidebarblock>.content>.title strong,h4 strong,h5 strong,h6 strong{font-weight:400}
.clearfix::before,.clearfix::after,.float-group::before,.float-group::after{content:" ";display:table}
.clearfix::after,.float-group::after{clear:both}
*:not(pre)>code{font-size:.9375em;font-style:normal!important;letter-spacing:0;padding:.1em .5ex;word-spacing:-.15em;background-color:#f7f7f8;-webkit-border-radius:4px;border-radius:4px;line-height:1.45;text-rendering:optimizeSpeed;word-wrap:break-word}
*:not(pre)>code.nobreak{word-wrap:normal}
*:not(pre)>code.nowrap{white-space:nowrap}
pre,pre>code{line-height:1.45;color:rgba(0,0,0,.9);font-family:"Droid Sans Mono","DejaVu Sans Mono",monospace;font-weight:400;text-rendering:optimizeSpeed}
em em{font-style:normal}
strong strong{font-weight:400}
.keyseq{color:rgba(51,51,51,.8)}
kbd{font-family:"Droid Sans Mono","DejaVu Sans Mono",monospace;display:inline-block;color:rgba(0,0,0,.8);font-size:.65em;line-height:1.45;background-color:#f7f7f7;border:1px solid #ccc;-webkit-border-radius:3px;border-radius:3px;-webkit-box-shadow:0 1px 0 rgba(0,0,0,.2),0 0 0 .1em white inset;box-shadow:0 1px 0 rgba(0,0,0,.2),0 0 0 .1em #fff inset;margin:0 .15em;padding:.2em .5em;vertical-align:middle;position:relative;top:-.1em;white-space:nowrap}
.keyseq kbd:first-child{margin-left:0}
.keyseq kbd:last-child{margin-right:0}
.menuseq,.menuref{color:#000}
.menuseq b:not(.caret),.menuref{font-weight:inherit}
.menuseq{word-spacing:-.02em}
.menuseq b.caret{font-size:1.25em;line-height:.8}
.menuseq i.caret{font-weight:bold;text-align:center;width:.45em}
b.button::before,b.button::after{position:relative;top:-1px;font-weight:400}
b.button::before{content:"[";padding:0 3px 0 2px}
b.button::after{content:"]";padding:0 2px 0 3px}
p a>code:hover{color:rgba(0,0,0,.9)}
#header,#content,#footnotes,#footer{width:100%;margin-left:auto;margin-right:auto;margin-top:0;margin-bottom:0;max-width:62.5em;*zoom:1;position:relative;padding-left:.9375em;padding-right:.9375em}
#header::before,#header::after,#content::before,#content::after,#footnotes::before,#footnotes::after,#footer::before,#footer::after{content:" ";display:table}
#header::after,#content::after,#footnotes::after,#footer::after{clear:both}
#content{margin-top:1.25em}
#content::before{content:none}
#header>h1:first-child{color:rgba(0,0,0,.85);margin-top:2.25rem;margin-bottom:0}
#header>h1:first-child+#toc{margin-top:8px;border-top:1px solid #dddddf}
#header>h1:only-child,body.toc2 #header>h1:nth-last-child(2){border-bottom:1px solid #dddddf;padding-bottom:8px}
#header .details{border-bottom:1px solid #dddddf;line-height:1.45;padding-top:.25em;padding-bottom:.25em;padding-left:.25em;color:rgba(0,0,0,.6);display:-ms-flexbox;display:-webkit-flex;display:flex;-ms-flex-flow:row wrap;-webkit-flex-flow:row wrap;flex-flow:row wrap}
#header .details span:first-child{margin-left:-.125em}
#header .details span.email a{color:rgba(0,0,0,.85)}
#header .details br{display:none}
#header .details br+span::before{content:"\00a0\2013\00a0"}
#header .details br+span.author::before{content:"\00a0\22c5\00a0";color:rgba(0,0,0,.85)}
#header .details br+span#revremark::before{content:"\00a0|\00a0"}
#header #revnumber{text-transform:capitalize}
#header #revnumber::after{content:"\00a0"}
#content>h1:first-child:not([class]){color:rgba(0,0,0,.85);border-bottom:1px solid #dddddf;padding-bottom:8px;margin-top:0;padding-top:1rem;margin-bottom:1.25rem}
#toc{border-bottom:1px solid #e7e7e9;padding-bottom:.5em}
#toc>ul{margin-left:.125em}
#toc ul.sectlevel0>li>a{font-style:italic}
#toc ul.sectlevel0 ul.sectlevel1{margin:.5em 0}
#toc ul{font-family:"Open Sans","DejaVu Sans",sans-serif;list-style-type:none}
#toc li{line-height:1.3334;margin-top:.3334em}
#toc a{text-decoration:none}
#toc a:active{text-decoration:underline}
#toctitle{color:#7a2518;font-size:1.2em}
@media screen and (min-width:768px){#toctitle{font-size:1.375em}
body.toc2{padding-left:15em;padding-right:0}
#toc.toc2{margin-top:0!important;background-color:#f8f8f7;position:fixed;width:15em;left:0;top:0;border-right:1px solid #e7e7e9;border-top-width:0!important;border-bottom-width:0!important;z-index:1000;padding:1.25em 1em;height:100%;overflow:auto}
#toc.toc2 #toctitle{margin-top:0;margin-bottom:.8rem;font-size:1.2em}
#toc.toc2>ul{font-size:.9em;margin-bottom:0}
#toc.toc2 ul ul{margin-left:0;padding-left:1em}
#toc.toc2 ul.sectlevel0 ul.sectlevel1{padding-left:0;margin-top:.5em;margin-bottom:.5em}
body.toc2.toc-right{padding-left:0;padding-right:15em}
body.toc2.toc-right #toc.toc2{border-right-width:0;border-left:1px solid #e7e7e9;left:auto;right:0}}
@media screen and (min-width:1280px){body.toc2{padding-left:20em;padding-right:0}
#toc.toc2{width:20em}
#toc.toc2 #toctitle{font-size:1.375em}
#toc.toc2>ul{font-size:.95em}
#toc.toc2 ul ul{padding-left:1.25em}
body.toc2.toc-right{padding-left:0;padding-right:20em}}
#content #toc{border-style:solid;border-width:1px;border-color:#e0e0dc;margin-bottom:1.25em;padding:1.25em;background:#f8f8f7;-webkit-border-radius:4px;border-radius:4px}
#content #toc>:first-child{margin-top:0}
#content #toc>:last-child{margin-bottom:0}
#footer{max-width:100%;background-color:rgba(0,0,0,.8);padding:1.25em}
#footer-text{color:rgba(255,255,255,.8);line-height:1.44}
#content{margin-bottom:.625em}
.sect1{padding-bottom:.625em}
@media screen and (min-width:768px){#content{margin-bottom:1.25em}
.sect1{padding-bottom:1.25em}}
.sect1:last-child{padding-bottom:0}
.sect1+.sect1{border-top:1px solid #e7e7e9}
#content h1>a.anchor,h2>a.anchor,h3>a.anchor,#toctitle>a.anchor,.sidebarblock>.content>.title>a.anchor,h4>a.anchor,h5>a.anchor,h6>a.anchor{position:absolute;z-index:1001;width:1.5ex;margin-left:-1.5ex;display:block;text-decoration:none!important;visibility:hidden;text-align:center;font-weight:400}
#content h1>a.anchor::before,h2>a.anchor::before,h3>a.anchor::before,#toctitle>a.anchor::before,.sidebarblock>.content>.title>a.anchor::before,h4>a.anchor::before,h5>a.anchor::before,h6>a.anchor::before{content:"\00A7";font-size:.85em;display:block;padding-top:.1em}
#content h1:hover>a.anchor,#content h1>a.anchor:hover,h2:hover>a.anchor,h2>a.anchor:hover,h3:hover>a.anchor,#toctitle:hover>a.anchor,.sidebarblock>.content>.title:hover>a.anchor,h3>a.anchor:hover,#toctitle>a.anchor:hover,.sidebarblock>.content>.title>a.anchor:hover,h4:hover>a.anchor,h4>a.anchor:hover,h5:hover>a.anchor,h5>a.anchor:hover,h6:hover>a.anchor,h6>a.anchor:hover{visibility:visible}
#content h1>a.link,h2>a.link,h3>a.link,#toctitle>a.link,.sidebarblock>.content>.title>a.link,h4>a.link,h5>a.link,h6>a.link{color:#ba3925;text-decoration:none}
#content h1>a.link:hover,h2>a.link:hover,h3>a.link:hover,#toctitle>a.link:hover,.sidebarblock>.content>.title>a.link:hover,h4>a.link:hover,h5>a.link:hover,h6>a.link:hover{color:#a53221}
.audioblock,.imageblock,.literalblock,.listingblock,.stemblock,.videoblock{margin-bottom:1.25em}
.admonitionblock td.content>.title,.audioblock>.title,.exampleblock>.title,.imageblock>.title,.listingblock>.title,.literalblock>.title,.stemblock>.title,.openblock>.title,.paragraph>.title,.quoteblock>.title,table.tableblock>.title,.verseblock>.title,.videoblock>.title,.dlist>.title,.olist>.title,.ulist>.title,.qlist>.title,.hdlist>.title{text-rendering:optimizeLegibility;text-align:left;font-family:"Noto Serif","DejaVu Serif",serif;font-size:1rem;font-style:italic}
table.tableblock.fit-content>caption.title{white-space:nowrap;width:0}
.paragraph.lead>p,#preamble>.sectionbody>[class="paragraph"]:first-of-type p{font-size:1.21875em;line-height:1.6;color:rgba(0,0,0,.85)}
table.tableblock #preamble>.sectionbody>[class="paragraph"]:first-of-type p{font-size:inherit}
.admonitionblock>table{border-collapse:separate;border:0;background:none;width:100%}
.admonitionblock>table td.icon{text-align:center;width:80px}
.admonitionblock>table td.icon img{max-width:none}
.admonitionblock>table td.icon .title{font-weight:bold;font-family:"Open Sans","DejaVu Sans",sans-serif;text-transform:uppercase}
.admonitionblock>table td.content{padding-left:1.125em;padding-right:1.25em;border-left:1px solid #dddddf;color:rgba(0,0,0,.6)}
.admonitionblock>table td.content>:last-child>:last-child{margin-bottom:0}
.exampleblock>.content{border-style:solid;border-width:1px;border-color:#e6e6e6;margin-bottom:1.25em;padding:1.25em;background:#fff;-webkit-border-radius:4px;border-radius:4px}
.exampleblock>.content>:first-child{margin-top:0}
.exampleblock>.content>:last-child{margin-bottom:0}
.sidebarblock{border-style:solid;border-width:1px;border-color:#e0e0dc;margin-bottom:1.25em;padding:1.25em;background:#f8f8f7;-webkit-border-radius:4px;border-radius:4px}
.sidebarblock>:first-child{margin-top:0}
.sidebarblock>:last-child{margin-bottom:0}
.sidebarblock>.content>.title{color:#7a2518;margin-top:0;text-align:center}
.exampleblock>.content>:last-child>:last-child,.exampleblock>.content .olist>ol>li:last-child>:last-child,.exampleblock>.content .ulist>ul>li:last-child>:last-child,.exampleblock>.content .qlist>ol>li:last-child>:last-child,.sidebarblock>.content>:last-child>:last-child,.sidebarblock>.content .olist>ol>li:last-child>:last-child,.sidebarblock>.content .ulist>ul>li:last-child>:last-child,.sidebarblock>.content .qlist>ol>li:last-child>:last-child{margin-bottom:0}
.literalblock pre,.listingblock pre:not(.highlight),.listingblock pre[class="highlight"],.listingblock pre[class^="highlight "],.listingblock pre.CodeRay,.listingblock pre.prettyprint{background:#f7f7f8}
.sidebarblock .literalblock pre,.sidebarblock .listingblock pre:not(.highlight),.sidebarblock .listingblock pre[class="highlight"],.sidebarblock .listingblock pre[class^="highlight "],.sidebarblock .listingblock pre.CodeRay,.sidebarblock .listingblock pre.prettyprint{background:#f2f1f1}
.literalblock pre,.literalblock pre[class],.listingblock pre,.listingblock pre[class]{-webkit-border-radius:4px;border-radius:4px;word-wrap:break-word;overflow-x:auto;padding:1em;font-size:.8125em}
@media screen and (min-width:768px){.literalblock pre,.literalblock pre[class],.listingblock pre,.listingblock pre[class]{font-size:.90625em}}
@media screen and (min-width:1280px){.literalblock pre,.literalblock pre[class],.listingblock pre,.listingblock pre[class]{font-size:1em}}
.literalblock pre.nowrap,.literalblock pre.nowrap pre,.listingblock pre.nowrap,.listingblock pre.nowrap pre{white-space:pre;word-wrap:normal}
.literalblock.output pre{color:#f7f7f8;background-color:rgba(0,0,0,.9)}
.listingblock pre.highlightjs{padding:0}
.listingblock pre.highlightjs>code{padding:1em;-webkit-border-radius:4px;border-radius:4px}
.listingblock pre.prettyprint{border-width:0}
.listingblock>.content{position:relative}
.listingblock code[data-lang]::before{display:none;content:attr(data-lang);position:absolute;font-size:.75em;top:.425rem;right:.5rem;line-height:1;text-transform:uppercase;color:#999}
.listingblock:hover code[data-lang]::before{display:block}
.listingblock.terminal pre .command::before{content:attr(data-prompt);padding-right:.5em;color:#999}
.listingblock.terminal pre .command:not([data-prompt])::before{content:"$"}
table.pyhltable{border-collapse:separate;border:0;margin-bottom:0;background:none}
table.pyhltable td{vertical-align:top;padding-top:0;padding-bottom:0;line-height:1.45}
table.pyhltable td.code{padding-left:.75em;padding-right:0}
pre.pygments .lineno,table.pyhltable td:not(.code){color:#999;padding-left:0;padding-right:.5em;border-right:1px solid #dddddf}
pre.pygments .lineno{display:inline-block;margin-right:.25em}
table.pyhltable .linenodiv{background:none!important;padding-right:0!important}
.quoteblock{margin:0 1em 1.25em 1.5em;display:table}
.quoteblock>.title{margin-left:-1.5em;margin-bottom:.75em}
.quoteblock blockquote,.quoteblock p{color:rgba(0,0,0,.85);font-size:1.15rem;line-height:1.75;word-spacing:.1em;letter-spacing:0;font-style:italic;text-align:justify}
.quoteblock blockquote{margin:0;padding:0;border:0}
.quoteblock blockquote::before{content:"\201c";float:left;font-size:2.75em;font-weight:bold;line-height:.6em;margin-left:-.6em;color:#7a2518;text-shadow:0 1px 2px rgba(0,0,0,.1)}
.quoteblock blockquote>.paragraph:last-child p{margin-bottom:0}
.quoteblock .attribution{margin-top:.75em;margin-right:.5ex;text-align:right}
.verseblock{margin:0 1em 1.25em}
.verseblock pre{font-family:"Open Sans","DejaVu Sans",sans;font-size:1.15rem;color:rgba(0,0,0,.85);font-weight:300;text-rendering:optimizeLegibility}
.verseblock pre strong{font-weight:400}
.verseblock .attribution{margin-top:1.25rem;margin-left:.5ex}
.quoteblock .attribution,.verseblock .attribution{font-size:.9375em;line-height:1.45;font-style:italic}
.quoteblock .attribution br,.verseblock .attribution br{display:none}
.quoteblock .attribution cite,.verseblock .attribution cite{display:block;letter-spacing:-.025em;color:rgba(0,0,0,.6)}
.quoteblock.abstract blockquote::before,.quoteblock.excerpt blockquote::before,.quoteblock .quoteblock blockquote::before{display:none}
.quoteblock.abstract blockquote,.quoteblock.abstract p,.quoteblock.excerpt blockquote,.quoteblock.excerpt p,.quoteblock .quoteblock blockquote,.quoteblock .quoteblock p{line-height:1.6;word-spacing:0}
.quoteblock.abstract{margin:0 1em 1.25em;display:block}
.quoteblock.abstract>.title{margin:0 0 .375em;font-size:1.15em;text-align:center}
.quoteblock.excerpt,.quoteblock .quoteblock{margin:0 0 1.25em;padding:0 0 .25em 1em;border-left:.25em solid #dddddf}
.quoteblock.excerpt blockquote,.quoteblock.excerpt p,.quoteblock .quoteblock blockquote,.quoteblock .quoteblock p{color:inherit;font-size:1.0625rem}
.quoteblock.excerpt .attribution,.quoteblock .quoteblock .attribution{color:inherit;text-align:left;margin-right:0}
table.tableblock{max-width:100%;border-collapse:separate}
p.tableblock:last-child{margin-bottom:0}
td.tableblock>.content{margin-bottom:-1.25em}
table.tableblock,th.tableblock,td.tableblock{border:0 solid #dedede}
table.grid-all>thead>tr>.tableblock,table.grid-all>tbody>tr>.tableblock{border-width:0 1px 1px 0}
table.grid-all>tfoot>tr>.tableblock{border-width:1px 1px 0 0}
table.grid-cols>*>tr>.tableblock{border-width:0 1px 0 0}
table.grid-rows>thead>tr>.tableblock,table.grid-rows>tbody>tr>.tableblock{border-width:0 0 1px}
table.grid-rows>tfoot>tr>.tableblock{border-width:1px 0 0}
table.grid-all>*>tr>.tableblock:last-child,table.grid-cols>*>tr>.tableblock:last-child{border-right-width:0}
table.grid-all>tbody>tr:last-child>.tableblock,table.grid-all>thead:last-child>tr>.tableblock,table.grid-rows>tbody>tr:last-child>.tableblock,table.grid-rows>thead:last-child>tr>.tableblock{border-bottom-width:0}
table.frame-all{border-width:1px}
table.frame-sides{border-width:0 1px}
table.frame-topbot,table.frame-ends{border-width:1px 0}
table.stripes-all tr,table.stripes-odd tr:nth-of-type(odd){background:#f8f8f7}
table.stripes-none tr,table.stripes-odd tr:nth-of-type(even){background:none}
th.halign-left,td.halign-left{text-align:left}
th.halign-right,td.halign-right{text-align:right}
th.halign-center,td.halign-center{text-align:center}
th.valign-top,td.valign-top{vertical-align:top}
th.valign-bottom,td.valign-bottom{vertical-align:bottom}
th.valign-middle,td.valign-middle{vertical-align:middle}
table thead th,table tfoot th{font-weight:bold}
tbody tr th{display:table-cell;line-height:1.6;background:#f7f8f7}
tbody tr th,tbody tr th p,tfoot tr th,tfoot tr th p{color:rgba(0,0,0,.8);font-weight:bold}
p.tableblock>code:only-child{background:none;padding:0}
p.tableblock{font-size:1em}
td>div.verse{white-space:pre}
ol{margin-left:1.75em}
ul li ol{margin-left:1.5em}
dl dd{margin-left:1.125em}
dl dd:last-child,dl dd:last-child>:last-child{margin-bottom:0}
ol>li p,ul>li p,ul dd,ol dd,.olist .olist,.ulist .ulist,.ulist .olist,.olist .ulist{margin-bottom:.625em}
ul.checklist,ul.none,ol.none,ul.no-bullet,ol.no-bullet,ol.unnumbered,ul.unstyled,ol.unstyled{list-style-type:none}
ul.no-bullet,ol.no-bullet,ol.unnumbered{margin-left:.625em}
ul.unstyled,ol.unstyled{margin-left:0}
ul.checklist{margin-left:.625em}
ul.checklist li>p:first-child>.fa-square-o:first-child,ul.checklist li>p:first-child>.fa-check-square-o:first-child{width:1.25em;font-size:.8em;position:relative;bottom:.125em}
ul.checklist li>p:first-child>input[type="checkbox"]:first-child{margin-right:.25em}
ul.inline{display:-ms-flexbox;display:-webkit-box;display:flex;-ms-flex-flow:row wrap;-webkit-flex-flow:row wrap;flex-flow:row wrap;list-style:none;margin:0 0 .625em -1.25em}
ul.inline>li{margin-left:1.25em}
.unstyled dl dt{font-weight:400;font-style:normal}
ol.arabic{list-style-type:decimal}
ol.decimal{list-style-type:decimal-leading-zero}
ol.loweralpha{list-style-type:lower-alpha}
ol.upperalpha{list-style-type:upper-alpha}
ol.lowerroman{list-style-type:lower-roman}
ol.upperroman{list-style-type:upper-roman}
ol.lowergreek{list-style-type:lower-greek}
.hdlist>table,.colist>table{border:0;background:none}
.hdlist>table>tbody>tr,.colist>table>tbody>tr{background:none}
td.hdlist1,td.hdlist2{vertical-align:top;padding:0 .625em}
td.hdlist1{font-weight:bold;padding-bottom:1.25em}
.literalblock+.colist,.listingblock+.colist{margin-top:-.5em}
.colist td:not([class]):first-child{padding:.4em .75em 0;line-height:1;vertical-align:top}
.colist td:not([class]):first-child img{max-width:none}
.colist td:not([class]):last-child{padding:.25em 0}
.thumb,.th{line-height:0;display:inline-block;border:solid 4px #fff;-webkit-box-shadow:0 0 0 1px #ddd;box-shadow:0 0 0 1px #ddd}
.imageblock.left{margin:.25em .625em 1.25em 0}
.imageblock.right{margin:.25em 0 1.25em .625em}
.imageblock>.title{margin-bottom:0}
.imageblock.thumb,.imageblock.th{border-width:6px}
.imageblock.thumb>.title,.imageblock.th>.title{padding:0 .125em}
.image.left,.image.right{margin-top:.25em;margin-bottom:.25em;display:inline-block;line-height:0}
.image.left{margin-right:.625em}
.image.right{margin-left:.625em}
a.image{text-decoration:none;display:inline-block}
a.image object{pointer-events:none}
sup.footnote,sup.footnoteref{font-size:.875em;position:static;vertical-align:super}
sup.footnote a,sup.footnoteref a{text-decoration:none}
sup.footnote a:active,sup.footnoteref a:active{text-decoration:underline}
#footnotes{padding-top:.75em;padding-bottom:.75em;margin-bottom:.625em}
#footnotes hr{width:20%;min-width:6.25em;margin:-.25em 0 .75em;border-width:1px 0 0}
#footnotes .footnote{padding:0 .375em 0 .225em;line-height:1.3334;font-size:.875em;margin-left:1.2em;margin-bottom:.2em}
#footnotes .footnote a:first-of-type{font-weight:bold;text-decoration:none;margin-left:-1.05em}
#footnotes .footnote:last-of-type{margin-bottom:0}
#content #footnotes{margin-top:-.625em;margin-bottom:0;padding:.75em 0}
.gist .file-data>table{border:0;background:#fff;width:100%;margin-bottom:0}
.gist .file-data>table td.line-data{width:99%}
div.unbreakable{page-break-inside:avoid}
.big{font-size:larger}
.small{font-size:smaller}
.underline{text-decoration:underline}
.overline{text-decoration:overline}
.line-through{text-decoration:line-through}
.aqua{color:#00bfbf}
.aqua-background{background-color:#00fafa}
.black{color:#000}
.black-background{background-color:#000}
.blue{color:#0000bf}
.blue-background{background-color:#0000fa}
.fuchsia{color:#bf00bf}
.fuchsia-background{background-color:#fa00fa}
.gray{color:#606060}
.gray-background{background-color:#7d7d7d}
.green{color:#006000}
.green-background{background-color:#007d00}
.lime{color:#00bf00}
.lime-background{background-color:#00fa00}
.maroon{color:#600000}
.maroon-background{background-color:#7d0000}
.navy{color:#000060}
.navy-background{background-color:#00007d}
.olive{color:#606000}
.olive-background{background-color:#7d7d00}
.purple{color:#600060}
.purple-background{background-color:#7d007d}
.red{color:#bf0000}
.red-background{background-color:#fa0000}
.silver{color:#909090}
.silver-background{background-color:#bcbcbc}
.teal{color:#006060}
.teal-background{background-color:#007d7d}
.white{color:#bfbfbf}
.white-background{background-color:#fafafa}
.yellow{color:#bfbf00}
.yellow-background{background-color:#fafa00}
span.icon>.fa{cursor:default}
a span.icon>.fa{cursor:inherit}
.admonitionblock td.icon [class^="fa icon-"]{font-size:2.5em;text-shadow:1px 1px 2px rgba(0,0,0,.5);cursor:default}
.admonitionblock td.icon .icon-note::before{content:"\f05a";color:#19407c}
.admonitionblock td.icon .icon-tip::before{content:"\f0eb";text-shadow:1px 1px 2px rgba(155,155,0,.8);color:#111}
.admonitionblock td.icon .icon-warning::before{content:"\f071";color:#bf6900}
.admonitionblock td.icon .icon-caution::before{content:"\f06d";color:#bf3400}
.admonitionblock td.icon .icon-important::before{content:"\f06a";color:#bf0000}
.conum[data-value]{display:inline-block;color:#fff!important;background-color:rgba(0,0,0,.8);-webkit-border-radius:100px;border-radius:100px;text-align:center;font-size:.75em;width:1.67em;height:1.67em;line-height:1.67em;font-family:"Open Sans","DejaVu Sans",sans-serif;font-style:normal;font-weight:bold}
.conum[data-value] *{color:#fff!important}
.conum[data-value]+b{display:none}
.conum[data-value]::after{content:attr(data-value)}
pre .conum[data-value]{position:relative;top:-.125em}
b.conum *{color:inherit!important}
.conum:not([data-value]):empty{display:none}
dt,th.tableblock,td.content,div.footnote{text-rendering:optimizeLegibility}
h1,h2,p,td.content,span.alt{letter-spacing:-.01em}
p strong,td.content strong,div.footnote strong{letter-spacing:-.005em}
p,blockquote,dt,td.content,span.alt{font-size:1.0625rem}
p{margin-bottom:1.25rem}
.sidebarblock p,.sidebarblock dt,.sidebarblock td.content,p.tableblock{font-size:1em}
.exampleblock>.content{background-color:#fffef7;border-color:#e0e0dc;-webkit-box-shadow:0 1px 4px #e0e0dc;box-shadow:0 1px 4px #e0e0dc}
.print-only{display:none!important}
@page{margin:1.25cm .75cm}
@media print{*{-webkit-box-shadow:none!important;box-shadow:none!important;text-shadow:none!important}
html{font-size:80%}
a{color:inherit!important;text-decoration:underline!important}
a.bare,a[href^="#"],a[href^="mailto:"]{text-decoration:none!important}
a[href^="http:"]:not(.bare)::after,a[href^="https:"]:not(.bare)::after{content:"(" attr(href) ")";display:inline-block;font-size:.875em;padding-left:.25em}
abbr[title]::after{content:" (" attr(title) ")"}
pre,blockquote,tr,img,object,svg{page-break-inside:avoid}
thead{display:table-header-group}
svg{max-width:100%}
p,blockquote,dt,td.content{font-size:1em;orphans:3;widows:3}
h2,h3,#toctitle,.sidebarblock>.content>.title{page-break-after:avoid}
#toc,.sidebarblock,.exampleblock>.content{background:none!important}
#toc{border-bottom:1px solid #dddddf!important;padding-bottom:0!important}
body.book #header{text-align:center}
body.book #header>h1:first-child{border:0!important;margin:2.5em 0 1em}
body.book #header .details{border:0!important;display:block;padding:0!important}
body.book #header .details span:first-child{margin-left:0!important}
body.book #header .details br{display:block}
body.book #header .details br+span::before{content:none!important}
body.book #toc{border:0!important;text-align:left!important;padding:0!important;margin:0!important}
body.book #toc,body.book #preamble,body.book h1.sect0,body.book .sect1>h2{page-break-before:always}
.listingblock code[data-lang]::before{display:block}
#footer{padding:0 .9375em}
.hide-on-print{display:none!important}
.print-only{display:block!important}
.hide-for-print{display:none!important}
.show-for-print{display:inherit!important}}
@media print,amzn-kf8{#header>h1:first-child{margin-top:1.25rem}
.sect1{padding:0!important}
.sect1+.sect1{border:0}
#footer{background:none}
#footer-text{color:rgba(0,0,0,.6);font-size:.9em}}
@media amzn-kf8{#header,#content,#footnotes,#footer{padding:0}}

      </style>
      <link href='https://fonts.googleapis.com/css?family=Noto+Serif' rel='stylesheet' type='text/css'>
      <link href='https://fonts.googleapis.com/css?family=Open+Sans:400,300,300italic,400italic,600,600italic,700,700italic,800,800italic' rel='stylesheet' type='text/css'>
      <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
      <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.9.0/styles/default.min.css">
      <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.9.0/highlight.min.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.9.0/languages/asciidoc.min.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.9.0/languages/yaml.min.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.9.0/languages/dockerfile.min.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.9.0/languages/makefile.min.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.9.0/languages/go.min.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.9.0/languages/rust.min.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.9.0/languages/haskell.min.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.9.0/languages/typescript.min.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.9.0/languages/scss.min.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.9.0/languages/less.min.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.9.0/languages/handlebars.min.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.9.0/languages/groovy.min.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.9.0/languages/scala.min.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.9.0/languages/bash.min.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.9.0/languages/ini.min.js"></script>
      <script>hljs.initHighlightingOnLoad();</script>
    </head>
    <body>
      <div id="wrapper">
        <div class="article">
          <h1>Spark Streaming</h1>
<div id="preamble">
<div class="sectionbody">
<div class="exampleblock">
<div class="title">导读</div>
<div class="content">
<div class="olist arabic">
<ol class="arabic">
<li>
<p>介绍</p>
</li>
<li>
<p>入门</p>
</li>
<li>
<p>原理</p>
</li>
<li>
<p>操作</p>
</li>
</ol>
</div>
</div>
</div>
</div>
<div id="toc" class="toc">
<div id="toctitle">Table of Contents</div>
<ul class="sectlevel1">
<li><a href="#_1_spark_streaming_介绍">1. Spark Streaming 介绍</a></li>
<li><a href="#_2_spark_streaming_入门">2. Spark Streaming 入门</a></li>
<li><a href="#_2_原理">2. 原理</a></li>
<li><a href="#_3_操作">3. 操作</a></li>
</ul>
</div>
</div>
<div class="sect1">
<h2 id="_1_spark_streaming_介绍">1. Spark Streaming 介绍</h2>
<div class="sectionbody">
<div class="exampleblock">
<div class="title">导读</div>
<div class="content">
<div class="olist arabic">
<ol class="arabic">
<li>
<p>流式计算的场景</p>
</li>
<li>
<p>流式计算框架</p>
</li>
<li>
<p><code>Spark Streaming</code> 的特点</p>
</li>
</ol>
</div>
</div>
</div>
<div class="dlist">
<dl>
<dt class="hdlist1">新的场景</dt>
<dd>
<div class="sidebarblock">
<div class="content">
<div class="paragraph">
<p>通过对现阶段一些常见的需求进行整理, 我们要问自己一个问题, 这些需求如何解决?</p>
</div>
<table class="tableblock frame-all grid-all stretch">
<colgroup>
<col style="width: 20%;">
<col>
</colgroup>
<thead>
<tr>
<th class="tableblock halign-left valign-top">场景</th>
<th class="tableblock halign-left valign-top">解释</th>
</tr>
</thead>
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">商品推荐</p></td>
<td class="tableblock halign-left valign-top"><div class="content"><div class="imageblock">
<div class="content">
<img src="https://doc-1256053707.cos.ap-beijing.myqcloud.com/20190618163602.png" alt="20190618163602" width="600">
</div>
</div>
<div class="imageblock">
<div class="content">
<img src="https://doc-1256053707.cos.ap-beijing.myqcloud.com/20190618163731.png" alt="20190618163731" width="600">
</div>
</div>
<div class="ulist">
<ul>
<li>
<p>京东和淘宝这样的商城在购物车, 商品详情等地方都有商品推荐的模块</p>
</li>
<li>
<p>商品推荐的要求</p>
<div class="ulist">
<ul>
<li>
<p>快速的处理, 加入购物车以后就需要迅速的进行推荐</p>
</li>
<li>
<p>数据量大</p>
</li>
<li>
<p>需要使用一些推荐算法</p>
</li>
</ul>
</div>
</li>
</ul>
</div></div></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">工业大数据</p></td>
<td class="tableblock halign-left valign-top"><div class="content"><div class="imageblock">
<div class="content">
<img src="https://doc-1256053707.cos.ap-beijing.myqcloud.com/20190618164233.png" alt="20190618164233" width="600">
</div>
</div>
<div class="ulist">
<ul>
<li>
<p>现在的工场中, 设备是可以联网的, 汇报自己的运行状态, 在应用层可以针对这些数据来分析运行状况和稳健程度, 展示工件完成情况, 运行情况等</p>
</li>
<li>
<p>工业大数据的需求</p>
<div class="ulist">
<ul>
<li>
<p>快速响应, 及时预测问题</p>
</li>
<li>
<p>数据是以事件的形式动态的产品和汇报</p>
</li>
<li>
<p>因为是运行状态信息, 而且一般都是几十上百台机器, 所以汇报的数据量很大</p>
</li>
</ul>
</div>
</li>
</ul>
</div></div></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">监控</p></td>
<td class="tableblock halign-left valign-top"><div class="content"><div class="imageblock">
<div class="content">
<img src="https://doc-1256053707.cos.ap-beijing.myqcloud.com/20190618164654.png" alt="20190618164654" width="600">
</div>
</div>
<div class="ulist">
<ul>
<li>
<p>一般的大型集群和平台, 都需要对其进行监控</p>
</li>
<li>
<p>监控的需求</p>
<div class="ulist">
<ul>
<li>
<p>要针对各种数据库, 包括 <code>MySQL</code>, <code>HBase</code> 等进行监控</p>
</li>
<li>
<p>要针对应用进行监控, 例如 <code>Tomcat</code>, <code>Nginx</code>, <code>Node.js</code> 等</p>
</li>
<li>
<p>要针对硬件的一些指标进行监控, 例如 <code>CPU</code>, 内存, 磁盘 等</p>
</li>
<li>
<p>这些工具的日志输出是非常多的, 往往一个用户的访问行为会带来几百条日志, 这些都要汇报, 所以数据量比较大</p>
</li>
<li>
<p>要从这些日志中, 聚合系统运行状况</p>
</li>
</ul>
</div>
</li>
</ul>
</div></div></td>
</tr>
</tbody>
</table>
<div class="admonitionblock note">
<table>
<tr>
<td class="icon">
<i class="fa icon-note" title="Note"></i>
</td>
<td class="content">
这样的需求, 可以通过传统的批处理来完成吗?
</td>
</tr>
</table>
</div>
</div>
</div>
</dd>
<dt class="hdlist1">流计算</dt>
<dd>
<div class="sidebarblock">
<div class="content">
<div class="ulist">
<ul>
<li>
<p>批量计算</p>
<div class="openblock">
<div class="content">
<div class="imageblock">
<div class="content">
<img src="https://doc-1256053707.cos.ap-beijing.myqcloud.com/20190619190216.png" alt="20190619190216" width="800">
</div>
</div>
<div class="paragraph">
<p>数据已经存在, 一次性读取所有的数据进行批量处理</p>
</div>
</div>
</div>
</li>
<li>
<p>流计算</p>
<div class="openblock">
<div class="content">
<div class="imageblock">
<div class="content">
<img src="https://doc-1256053707.cos.ap-beijing.myqcloud.com/20190619190515.png" alt="20190619190515" width="800">
</div>
</div>
<div class="paragraph">
<p>数据源源不断的进来, 经过处理后落地</p>
</div>
</div>
</div>
</li>
</ul>
</div>
</div>
</div>
</dd>
<dt class="hdlist1">流和批的架构组合</dt>
<dd>
<div class="sidebarblock">
<div class="content">
<div class="paragraph">
<p>流和批都是有意义的, 有自己的应用场景, 那么如何结合流和批呢? 如何在同一个系统中使用这两种不同的解决方案呢?</p>
</div>
<div class="dlist">
<dl>
<dt class="hdlist1">混合架构</dt>
<dd>
<div class="exampleblock">
<div class="content">
<div class="imageblock">
<div class="content">
<img src="https://doc-1256053707.cos.ap-beijing.myqcloud.com/20190618165326.png" alt="20190618165326" width="800">
</div>
</div>
<div class="ulist">
<ul>
<li>
<p>混合架构说明</p>
<div class="openblock">
<div class="content">
<div class="paragraph">
<p>混合架构的名字叫做 <code>Lambda 架构</code>, 混合架构最大的特点就是将流式计算和批处理结合起来</p>
</div>
<div class="paragraph">
<p>后在进行查询的时候分别查询流系统和批系统, 最后将结果合并在一起</p>
</div>
<div class="imageblock">
<div class="content">
<img src="https://doc-1256053707.cos.ap-beijing.myqcloud.com/20190618171909.png" alt="20190618171909" width="500">
</div>
</div>
<div class="paragraph">
<p>一般情况下 Lambda 架构分三层</p>
</div>
<div class="ulist">
<ul>
<li>
<p>批处理层: 批量写入, 批量读取</p>
</li>
<li>
<p>服务层: 分为两个部分, 一部分对应批处理层, 一部分对应速度层</p>
</li>
<li>
<p>速度层: 随机读取, 随即写入, 增量计算</p>
</li>
</ul>
</div>
</div>
</div>
</li>
<li>
<p>优点</p>
<div class="openblock">
<div class="content">
<div class="ulist">
<ul>
<li>
<p>兼顾优点, 在批处理层可以全量查询和分析, 在速度层可以查询最新的数据</p>
</li>
<li>
<p>速度很快, 在大数据系统中, 想要快速的获取结果是非常困难的, 因为高吞吐量和快速返回结果往往很难兼得, 例如 <code>Impala</code> 和 <code>Hive</code>, <code>Hive</code> 能进行非常大规模的数据量的处理, <code>Impala</code> 能够快速的查询返回结果, 但是很少有一个系统能够兼得两点, <code>Lambda</code> 使用多种融合的手段从而实现</p>
</li>
</ul>
</div>
</div>
</div>
</li>
<li>
<p>缺点</p>
<div class="openblock">
<div class="content">
<div class="paragraph">
<p><code>Lambda</code> 是一个非常反人类的设计, 因为我们需要在系统中不仅维护多套数据层, 还需要维护批处理和流式处理两套框架, 这非常困难, 一套都很难搞定, 两套带来的运维问题是是指数级提升的</p>
</div>
</div>
</div>
</li>
</ul>
</div>
</div>
</div>
</dd>
<dt class="hdlist1">流式架构</dt>
<dd>
<div class="exampleblock">
<div class="content">
<div class="imageblock">
<div class="content">
<img src="https://doc-1256053707.cos.ap-beijing.myqcloud.com/20190618165455.png" alt="20190618165455" width="600">
</div>
</div>
<div class="ulist">
<ul>
<li>
<p>流式架构说明</p>
<div class="openblock">
<div class="content">
<div class="paragraph">
<p>流式架构常见的叫做 <code>Kappa 结构</code>, 是 <code>Lambda 架构</code> 的一个变种, 其实本质上就是删掉了批处理</p>
</div>
</div>
</div>
</li>
<li>
<p>优点</p>
<div class="openblock">
<div class="content">
<div class="ulist">
<ul>
<li>
<p>非常简单</p>
</li>
<li>
<p>效率很高, 在存储系统的发展下, 很多存储系统已经即能快速查询又能批量查询了, 所以 <code>Kappa 架构</code> 在新时代还是非常够用的</p>
</li>
</ul>
</div>
</div>
</div>
</li>
<li>
<p>问题</p>
<div class="openblock">
<div class="content">
<div class="paragraph">
<p>丧失了一些 <code>Lambda</code> 的优秀特点</p>
</div>
</div>
</div>
</li>
</ul>
</div>
</div>
</div>
</dd>
</dl>
</div>
<div class="paragraph">
<p>关于架构的问题, 很多时候往往是无解的, 在合适的地方使用合适的架构, 在项目课程中, 还会进行更细致的讨论</p>
</div>
</div>
</div>
</dd>
<dt class="hdlist1"><code>Spark Streaming</code> 的特点</dt>
<dd>
<div class="sidebarblock">
<div class="content">
<table class="tableblock frame-all grid-all stretch">
<colgroup>
<col style="width: 50%;">
<col style="width: 50%;">
</colgroup>
<thead>
<tr>
<th class="tableblock halign-left valign-top">特点</th>
<th class="tableblock halign-left valign-top">说明</th>
</tr>
</thead>
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>Spark Streaming</code> 是 <code>Spark Core API</code> 的扩展</p></td>
<td class="tableblock halign-left valign-top"><div class="content"><div class="ulist">
<ul>
<li>
<p><code>Spark Streaming</code> 具有类似 <code>RDD</code> 的 <code>API</code>, 易于使用, 并可和现有系统共用相似代码</p>
</li>
<li>
<p>一个非常重要的特点是, <code>Spark Streaming</code> 可以在流上使用基于 <code>Spark</code> 的机器学习和流计算, 是一个一站式的平台</p>
</li>
</ul>
</div></div></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>Spark Streaming</code> 具有很好的整合性</p></td>
<td class="tableblock halign-left valign-top"><div class="content"><div class="ulist">
<ul>
<li>
<p><code>Spark Streaming</code> 可以从 <code>Kafka</code>, <code>Flume</code>, <code>TCP</code> 等流和队列中获取数据</p>
</li>
<li>
<p><code>Spark Streaming</code> 可以将处理过的数据写入文件系统, 常见数据库中</p>
</li>
</ul>
</div></div></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>Spark Streaming</code> 是微批次处理模型</p></td>
<td class="tableblock halign-left valign-top"><div class="content"><div class="ulist">
<ul>
<li>
<p>微批次处理的方式不会有长时间运行的 <code>Operator</code>, 所以更易于容错设计</p>
</li>
<li>
<p>微批次模型能够避免运行过慢的服务, 实行推测执行</p>
</li>
</ul>
</div></div></td>
</tr>
</tbody>
</table>
</div>
</div>
</dd>
</dl>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_2_spark_streaming_入门">2. Spark Streaming 入门</h2>
<div class="sectionbody">
<div class="exampleblock">
<div class="title">导读</div>
<div class="content">
<div class="olist arabic">
<ol class="arabic">
<li>
<p>环境准备</p>
</li>
<li>
<p>工程搭建</p>
</li>
<li>
<p>代码编写</p>
</li>
<li>
<p>总结</p>
</li>
</ol>
</div>
</div>
</div>
<div class="dlist">
<dl>
<dt class="hdlist1"><code>Netcat</code> 的使用</dt>
<dd>
<div class="sidebarblock">
<div class="content">
<div class="dlist">
<dl>
<dt class="hdlist1"><code>Step 1</code>: <code>Socket</code> 回顾</dt>
<dd>
<div class="exampleblock">
<div class="content">
<div class="imageblock">
<div class="content">
<img src="https://doc-1256053707.cos.ap-beijing.myqcloud.com/20190618232336.png" alt="20190618232336" width="800">
</div>
</div>
<div class="ulist">
<ul>
<li>
<p><code>Socket</code> 是 <code>Java</code> 中为了支持基于 <code>TCP / UDP</code> 协议的通信所提供的编程模型</p>
</li>
<li>
<p><code>Socket</code> 分为 <code>Socket server</code> 和 <code>Socket client</code></p>
<div class="openblock">
<div class="content">
<div class="dlist">
<dl>
<dt class="hdlist1"><code>Socket server</code></dt>
<dd>
<p>监听某个端口, 接收 <code>Socket client</code> 发过来的连接请求建立连接, 连接建立后可以向 <code>Socket client</code> 发送 <code>TCP packet</code> 交互 (被动)</p>
</dd>
<dt class="hdlist1"><code>Socket client</code></dt>
<dd>
<p>向某个端口发起连接, 并在连接建立后, 向 <code>Socket server</code> 发送 <code>TCP packet</code> 实现交互 (主动)</p>
</dd>
</dl>
</div>
</div>
</div>
</li>
<li>
<p><code>TCP</code> 三次握手建立连接</p>
<div class="openblock">
<div class="content">
<div class="dlist">
<dl>
<dt class="hdlist1"><code>Step 1</code></dt>
<dd>
<p><code>Client</code> 向 <code>Server</code> 发送 <code>SYN(j)</code>, 进入 <code>SYN_SEND</code> 状态等待 <code>Server</code> 响应</p>
</dd>
<dt class="hdlist1"><code>Step 2</code></dt>
<dd>
<p><code>Server</code> 收到 <code>Client</code> 的 <code>SYN(j)</code> 并发送确认包 <code>ACK(j + 1)</code>, 同时自己也发送一个请求连接的 <code>SYN(k)</code> 给 <code>Client</code>, 进入 <code>SYN_RECV</code> 状态等待 <code>Client</code> 确认</p>
</dd>
<dt class="hdlist1"><code>Step 3</code></dt>
<dd>
<p><code>Client</code> 收到 <code>Server</code> 的 <code>ACK + SYN</code>, 向 <code>Server</code> 发送连接确认 <code>ACK(k + 1)</code>, 此时, <code>Client</code> 和 <code>Server</code> 都进入 <code>ESTABLISHED</code> 状态, 准备数据发送</p>
</dd>
</dl>
</div>
</div>
</div>
</li>
</ul>
</div>
</div>
</div>
</dd>
<dt class="hdlist1"><code>Step 2:</code> <code>Netcat</code></dt>
<dd>
<div class="exampleblock">
<div class="content">
<div class="imageblock">
<div class="content">
<img src="https://doc-1256053707.cos.ap-beijing.myqcloud.com/20190619003144.png" alt="20190619003144" width="800">
</div>
</div>
<div class="ulist">
<ul>
<li>
<p><code>Netcat</code> 简写 <code>nc</code>, 命令行中使用 <code>nc</code> 命令调用</p>
</li>
<li>
<p><code>Netcat</code> 是一个非常常见的 <code>Socket</code> 工具, 可以使用 <code>nc</code> 建立 <code>Socket server</code> 也可以建立 <code>Socket client</code></p>
<div class="ulist">
<ul>
<li>
<p><code>nc -l</code> 建立 <code>Socket server</code>, <code>l</code> 是 <code>listen</code> 监听的意思</p>
</li>
<li>
<p><code>nc host port</code> 建立 <code>Socket client</code>, 并连接到某个 <code>Socket server</code></p>
</li>
</ul>
</div>
</li>
</ul>
</div>
</div>
</div>
</dd>
</dl>
</div>
</div>
</div>
</dd>
<dt class="hdlist1">创建工程</dt>
<dd>
<div class="sidebarblock">
<div class="content">
<div class="dlist">
<dl>
<dt class="hdlist1">目标</dt>
<dd>
<div class="exampleblock">
<div class="content">
<div class="paragraph">
<p>使用 <code>Spark Streaming</code> 程序和 <code>Socket server</code> 进行交互, 从 <code>Server</code> 处获取实时传输过来的字符串, 拆开单词并统计单词数量, 最后打印出来每一个小批次的单词数量</p>
</div>
<div class="imageblock">
<div class="content">
<img src="https://doc-1256053707.cos.ap-beijing.myqcloud.com/20190619184222.png" alt="20190619184222" width="800">
</div>
</div>
</div>
</div>
</dd>
<dt class="hdlist1"><code>Step 1:</code> 创建工程</dt>
<dd>
<div class="exampleblock">
<div class="content">
<div class="olist arabic">
<ol class="arabic">
<li>
<p>创建 <code>IDEA Maven</code> 工程, 步骤省略, 参考 <code>Spark</code> 第一天工程建立方式</p>
</li>
<li>
<p>导入 <code>Maven</code> 依赖, 省略, 参考 <code>Step 2</code></p>
</li>
<li>
<p>创建 <code>main/scala</code> 文件夹和 <code>test/scala</code> 文件夹</p>
</li>
<li>
<p>创建包 <code>cn.itcast.streaming</code></p>
</li>
<li>
<p>创建对象 <code>StreamingWordCount</code></p>
</li>
</ol>
</div>
</div>
</div>
</dd>
<dt class="hdlist1"><code>Step 2:</code> <code>Maven</code> 依赖</dt>
<dd>
<div class="exampleblock">
<div class="content">
<div class="paragraph">
<p>如果使用 <code>Spark Streaming</code>, 需要使用如下 <code>Spark</code> 的依赖</p>
</div>
<div class="ulist">
<ul>
<li>
<p><code>Spark Core</code>: <code>Spark</code> 的核心包, 因为 <code>Spark Streaming</code> 要用到</p>
</li>
<li>
<p><code>Spark Streaming</code></p>
</li>
</ul>
</div>
</div>
</div>
</dd>
<dt class="hdlist1"><code>Step 3:</code> 编码</dt>
<dd>
<div class="exampleblock">
<div class="content">
<div class="listingblock">
<div class="content">
<pre class="highlightjs highlight"><code class="language-scala hljs" data-lang="scala">object StreamingWordCount {

  def main(args: Array[String]): Unit = {
    if (args.length &lt; 2) {
      System.err.println("Usage: NetworkWordCount &lt;hostname&gt; &lt;port&gt;")
      System.exit(1)
    }

    val sparkConf = new SparkConf().setAppName("NetworkWordCount")
    val ssc = new StreamingContext(sparkConf, Seconds(1))              <i class="conum" data-value="1"></i><b>(1)</b>

    val lines = ssc.socketTextStream(                                  <i class="conum" data-value="2"></i><b>(2)</b>
      hostname = args(0),
      port = args(1).toInt,
      storageLevel = StorageLevel.MEMORY_AND_DISK_SER)                 <i class="conum" data-value="3"></i><b>(3)</b>

    val words = lines.flatMap(_.split(" "))
    val wordCounts = words.map(x =&gt; (x, 1)).reduceByKey(_ + _)

    wordCounts.print()                                                 <i class="conum" data-value="4"></i><b>(4)</b>

    ssc.start()                                                        <i class="conum" data-value="5"></i><b>(5)</b>
    ssc.awaitTermination()                                             <i class="conum" data-value="6"></i><b>(6)</b>
  }
}</code></pre>
</div>
</div>
<div class="colist arabic">
<table>
<tr>
<td><i class="conum" data-value="1"></i><b>1</b></td>
<td>在 <code>Spark</code> 中, 一般使用 <code>XXContext</code> 来作为入口, <code>Streaming</code> 也不例外, 所以创建 <code>StreamingContext</code> 就是创建入口</td>
</tr>
<tr>
<td><i class="conum" data-value="2"></i><b>2</b></td>
<td>开启 <code>Socket</code> 的 <code>Receiver</code>, 连接到某个 <code>TCP</code> 端口, 作为 <code>Socket client</code>, 去获取数据</td>
</tr>
<tr>
<td><i class="conum" data-value="3"></i><b>3</b></td>
<td>选择 <code>Receiver</code> 获取到数据后的保存方式, 此处是内存和磁盘都有, 并且序列化后保存</td>
</tr>
<tr>
<td><i class="conum" data-value="4"></i><b>4</b></td>
<td>类似 <code>RDD</code> 中的 <code>Action</code>, 执行最后的数据输出和收集</td>
</tr>
<tr>
<td><i class="conum" data-value="5"></i><b>5</b></td>
<td>启动流和 <code>JobGenerator</code>, 开始流式处理数据</td>
</tr>
<tr>
<td><i class="conum" data-value="6"></i><b>6</b></td>
<td>阻塞主线程, 后台线程开始不断获取数据并处理</td>
</tr>
</table>
</div>
</div>
</div>
</dd>
<dt class="hdlist1"><code>Step 4:</code> 部署和上线</dt>
<dd>
<div class="exampleblock">
<div class="content">
<div class="olist arabic">
<ol class="arabic">
<li>
<p>使用 Maven 命令 package 打包</p>
<div class="imageblock">
<div class="content">
<img src="https://doc-1256053707.cos.ap-beijing.myqcloud.com/20190619182630.png" alt="20190619182630" width="400">
</div>
</div>
</li>
<li>
<p>将打好的包上传到 <code>node01</code></p>
<div class="imageblock">
<div class="content">
<img src="https://doc-1256053707.cos.ap-beijing.myqcloud.com/20190619183133.png" alt="20190619183133" width="300">
</div>
</div>
</li>
<li>
<p>在 <code>node02</code> 上使用 <code>nc</code> 开启一个 <code>Socket server</code>, 接受 <code>Streaming</code> 程序的连接请求, 从而建立连接发送消息给 <code>Streaming</code> 程序实时处理</p>
<div class="listingblock">
<div class="content">
<pre>nc -lk 9999</pre>
</div>
</div>
</li>
<li>
<p>在 <code>node01</code> 执行如下命令运行程序</p>
<div class="listingblock">
<div class="content">
<pre class="highlightjs highlight"><code class="language-text hljs" data-lang="text">spark-submit --class cn.itcast.streaming.StreamingWordCount  --master local[6] original-streaming-0.0.1.jar node02 9999</code></pre>
</div>
</div>
</li>
</ol>
</div>
</div>
</div>
</dd>
</dl>
</div>
</div>
</div>
</dd>
<dt class="hdlist1"><code>Step 5:</code> 总结和知识落地</dt>
<dd>
<div class="sidebarblock">
<div class="content">
<div class="dlist">
<dl>
<dt class="hdlist1">注意点</dt>
<dd>
<div class="exampleblock">
<div class="content">
<div class="ulist">
<ul>
<li>
<p><code>Spark Streaming</code> 并不是真正的来一条数据处理一条</p>
<div class="openblock">
<div class="content">
<div class="imageblock">
<div class="content">
<img src="https://doc-1256053707.cos.ap-beijing.myqcloud.com/20190620005146.png" alt="20190620005146" width="600">
</div>
</div>
<div class="paragraph">
<p><code>Spark Streaming</code> 的处理机制叫做小批量, 英文叫做 <code>mini-batch</code>, 是收集了一定时间的数据后生成 <code>RDD</code>, 后针对 <code>RDD</code> 进行各种转换操作, 这个原理提现在如下两个地方</p>
</div>
<div class="ulist">
<ul>
<li>
<p>控制台中打印的结果是一个批次一个批次的, 统计单词数量也是按照一个批次一个批次的统计</p>
</li>
<li>
<p>多长时间生成一个 <code>RDD</code> 去统计呢? 由 <code>new StreamingContext(sparkConf, Seconds(1))</code> 这段代码中的第二个参数指定批次生成的时间</p>
</li>
</ul>
</div>
</div>
</div>
</li>
<li>
<p><code>Spark Streaming</code> 中至少要有两个线程</p>
<div class="openblock">
<div class="content">
<div class="paragraph">
<p>在使用 <code>spark-submit</code> 启动程序的时候, 不能指定一个线程</p>
</div>
<div class="ulist">
<ul>
<li>
<p>主线程被阻塞了, 等待程序运行</p>
</li>
<li>
<p>需要开启后台线程获取数据</p>
</li>
</ul>
</div>
</div>
</div>
</li>
</ul>
</div>
</div>
</div>
</dd>
<dt class="hdlist1">创建 <code>StreamingContext</code></dt>
<dd>
<div class="exampleblock">
<div class="content">
<div class="listingblock">
<div class="content">
<pre class="highlightjs highlight"><code class="language-scala hljs" data-lang="scala">val conf = new SparkConf().setAppName(appName).setMaster(master)
val ssc = new StreamingContext(conf, Seconds(1))</code></pre>
</div>
</div>
<div class="ulist">
<ul>
<li>
<p><code>StreamingContext</code> 是 <code>Spark Streaming</code> 程序的入口</p>
</li>
<li>
<p>在创建 <code>StreamingContext</code> 的时候, 必须要指定两个参数, 一个是 <code>SparkConf</code>, 一个是流中生成 <code>RDD</code> 的时间间隔</p>
</li>
<li>
<p><code>StreamingContext</code> 提供了如下功能</p>
<div class="ulist">
<ul>
<li>
<p>创建 <code>DStream</code>, 可以通过读取 <code>Kafka</code>, 读取 <code>Socket</code> 消息, 读取本地文件等创建一个流, 并且作为整个 <code>DAG</code> 中的 <code>InputDStream</code></p>
</li>
<li>
<p><code>RDD</code> 遇到 <code>Action</code> 才会执行, 但是 <code>DStream</code> 不是, <code>DStream</code> 只有在 <code>StreamingContext.start()</code> 后才会开始接收数据并处理数据</p>
</li>
<li>
<p>使用 <code>StreamingContext.awaitTermination()</code> 等待处理被终止</p>
</li>
<li>
<p>使用 <code>StreamingContext.stop()</code> 来手动的停止处理</p>
</li>
</ul>
</div>
</li>
<li>
<p>在使用的时候有如下注意点</p>
<div class="ulist">
<ul>
<li>
<p>同一个 <code>Streaming</code> 程序中, 只能有一个 <code>StreamingContext</code></p>
</li>
<li>
<p>一旦一个 <code>Context</code> 已经启动 (<code>start</code>), 则不能添加新的数据源
**</p>
</li>
</ul>
</div>
</li>
</ul>
</div>
</div>
</div>
</dd>
<dt class="hdlist1">各种算子</dt>
<dd>
<div class="exampleblock">
<div class="content">
<div class="imageblock">
<div class="content">
<img src="https://doc-1256053707.cos.ap-beijing.myqcloud.com/20190620005229.png" alt="20190620005229" width="600">
</div>
</div>
<div class="ulist">
<ul>
<li>
<p>这些算子类似 <code>RDD</code>, 也会生成新的 <code>DStream</code></p>
</li>
<li>
<p>这些算子操作最终会落到每一个 <code>DStream</code> 生成的 <code>RDD</code> 中</p>
</li>
</ul>
</div>
<table class="tableblock frame-all grid-all stretch">
<colgroup>
<col style="width: 20%;">
<col>
</colgroup>
<thead>
<tr>
<th class="tableblock halign-left valign-top">算子</th>
<th class="tableblock halign-left valign-top">释义</th>
</tr>
</thead>
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>flatMap</code></p></td>
<td class="tableblock halign-left valign-top"><div class="content"><div class="listingblock">
<div class="content">
<pre class="highlightjs highlight"><code class="language-scala hljs" data-lang="scala">lines.flatMap(_.split(" "))</code></pre>
</div>
</div>
<div class="paragraph">
<p>将一个数据一对多的转换为另外的形式, 规则通过传入函数指定</p>
</div></div></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>map</code></p></td>
<td class="tableblock halign-left valign-top"><div class="content"><div class="listingblock">
<div class="content">
<pre class="highlightjs highlight"><code class="language-scala hljs" data-lang="scala">words.map(x =&gt; (x, 1))</code></pre>
</div>
</div>
<div class="paragraph">
<p>一对一的转换数据</p>
</div></div></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>reduceByKey</code></p></td>
<td class="tableblock halign-left valign-top"><div class="content"><div class="listingblock">
<div class="content">
<pre class="highlightjs highlight"><code class="language-scala hljs" data-lang="scala">words.reduceByKey(_ + _)</code></pre>
</div>
</div>
<div class="paragraph">
<p>这个算子需要特别注意, 这个聚合并不是针对于整个流, 而是针对于某个批次的数据</p>
</div></div></td>
</tr>
</tbody>
</table>
</div>
</div>
</dd>
</dl>
</div>
</div>
</div>
</dd>
</dl>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_2_原理">2. 原理</h2>
<div class="sectionbody">
<div class="exampleblock">
<div class="content">
<div class="olist arabic">
<ol class="arabic">
<li>
<p>总章</p>
</li>
<li>
<p>静态 <code>DAG</code></p>
</li>
<li>
<p>动态切分</p>
</li>
<li>
<p>数据流入</p>
</li>
<li>
<p>容错机制</p>
</li>
</ol>
</div>
</div>
</div>
<div class="dlist">
<dl>
<dt class="hdlist1">总章</dt>
<dd>
<div class="sidebarblock">
<div class="content">
<div class="dlist">
<dl>
<dt class="hdlist1"><code>Spark Streaming</code> 的特点</dt>
<dd>
<div class="exampleblock">
<div class="content">
<div class="ulist">
<ul>
<li>
<p><code>Spark Streaming</code> 会源源不断的处理数据, 称之为流计算</p>
</li>
<li>
<p><code>Spark Streaming</code> 并不是实时流, 而是按照时间切分小批量, 一个一个的小批量处理</p>
</li>
<li>
<p><code>Spark Streaming</code> 是流计算, 所以可以理解为数据会源源不断的来, 需要长时间运行</p>
</li>
</ul>
</div>
</div>
</div>
</dd>
<dt class="hdlist1"><code>Spark Streaming</code> 是按照时间切分小批量</dt>
<dd>
<div class="exampleblock">
<div class="content">
<div class="ulist">
<ul>
<li>
<p>如何小批量?</p>
<div class="openblock">
<div class="content">
<div class="paragraph">
<p><code>Spark Streaming</code> 中的编程模型叫做 <code>DStream</code>, 所有的 <code>API</code> 都从 <code>DStream</code> 开始, 其作用就类似于 <code>RDD</code> 之于 <code>Spark Core</code></p>
</div>
<div class="imageblock">
<div class="content">
<img src="https://doc-1256053707.cos.ap-beijing.myqcloud.com/20190619201930.png" alt="20190619201930" width="800">
</div>
</div>
<div class="paragraph">
<p>可以理解为 <code>DStream</code> 是一个管道, 数据源源不断的从这个管道进去, 被处理, 再出去</p>
</div>
<div class="imageblock">
<div class="content">
<img src="https://doc-1256053707.cos.ap-beijing.myqcloud.com/20190619202422.png" alt="20190619202422" width="800">
</div>
</div>
<div class="paragraph">
<p>但是需要注意的是, <code>DStream</code> 并不是严格意义上的实时流, 事实上, <code>DStream</code> 并不处理数据, 而是处理 <code>RDD</code></p>
</div>
<div class="imageblock">
<div class="content">
<img src="https://doc-1256053707.cos.ap-beijing.myqcloud.com/20190619202628.png" alt="20190619202628" width="800">
</div>
</div>
<div class="paragraph">
<p>以上, 可以整理出如下道理</p>
</div>
<div class="ulist">
<ul>
<li>
<p><code>Spark Streaming</code> 是小批量处理数据, 并不是实时流</p>
</li>
<li>
<p><code>Spark Streaming</code> 对数据的处理是按照时间切分为一个又一个小的 <code>RDD</code>, 然后针对 <code>RDD</code> 进行处理</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>所以针对以上的解读, 可能会产生一种疑惑</p>
</div>
<div class="ulist">
<ul>
<li>
<p>如何切分 <code>RDD</code>?</p>
</li>
</ul>
</div>
</div>
</div>
</li>
<li>
<p>如何处理数据?</p>
<div class="openblock">
<div class="content">
<div class="paragraph">
<p>如下代码</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlightjs highlight"><code class="language-scala hljs" data-lang="scala">val lines: DStream[String] = ssc.socketTextStream(
  hostname = args(0),
  port = args(1).toInt,
  storageLevel = StorageLevel.MEMORY_AND_DISK_SER)

val words: DStream[String] = lines
  .flatMap(_.split(" "))
  .map(x =&gt; (x, 1))
  .reduceByKey(_ + _)</code></pre>
</div>
</div>
<div class="paragraph">
<p>可以看到</p>
</div>
<div class="ulist">
<ul>
<li>
<p><code>RDD</code> 中针对数据的处理是使用算子, 在 <code>DStream</code> 中针对数据的操作也是算子</p>
</li>
<li>
<p><code>DStream</code> 的算子似乎和 <code>RDD</code> 没什么区别</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>有一个疑惑</p>
</div>
<div class="ulist">
<ul>
<li>
<p>难道 <code>DStream</code> 会把算子的操作交给 <code>RDD</code> 去处理? 如何交?</p>
</li>
</ul>
</div>
</div>
</div>
</li>
</ul>
</div>
</div>
</div>
</dd>
<dt class="hdlist1"><code>Spark Streaming</code> 是流计算, 流计算的数据是无限的</dt>
<dd>
<div class="exampleblock">
<div class="content">
<div class="paragraph">
<p>什么系统可以产生无限的数据?</p>
</div>
<div class="imageblock">
<div class="content">
<img src="https://doc-1256053707.cos.ap-beijing.myqcloud.com/20190619190515.png" alt="20190619190515" width="800">
</div>
</div>
<div class="paragraph">
<p>无限的数据一般指的是数据不断的产生, 比如说运行中的系统, 无法判定什么时候公司会倒闭, 所以也无法断定数据什么时候会不再产生数据</p>
</div>
<div class="dlist">
<dl>
<dt class="hdlist1">那就会产生一个问题</dt>
<dd>
<p>如何不简单的读取数据, 如何应对数据量时大时小?</p>
</dd>
</dl>
</div>
<div class="paragraph">
<p>如何数据是无限的, 意味着可能要一直运行下去</p>
</div>
<div class="dlist">
<dl>
<dt class="hdlist1">那就会又产生一个问题</dt>
<dd>
<p><code>Spark Streaming</code> 不会出错吗? 数据出错了怎么办?</p>
</dd>
</dl>
</div>
</div>
</div>
</dd>
<dt class="hdlist1">总结</dt>
<dd>
<div class="exampleblock">
<div class="content">
<div class="paragraph">
<p>总结下来, 有四个问题</p>
</div>
<div class="ulist">
<ul>
<li>
<p><code>DStream</code> 如何对应 <code>RDD</code>?</p>
</li>
<li>
<p>如何切分 <code>RDD</code>?</p>
</li>
<li>
<p>如何读取数据?</p>
</li>
<li>
<p>如何容错?</p>
</li>
</ul>
</div>
</div>
</div>
</dd>
</dl>
</div>
</div>
</div>
</dd>
<dt class="hdlist1"><code>DAG</code> 的定义</dt>
<dd>
<div class="sidebarblock">
<div class="content">
<div class="dlist">
<dl>
<dt class="hdlist1"><code>RDD</code> 和 <code>DStream</code> 的 <code>DAG</code></dt>
<dd>
<div class="exampleblock">
<div class="content">
<div class="paragraph">
<p>如果是 <code>RDD</code> 的 <code>WordCount</code>, 代码大致如下</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlightjs highlight"><code class="language-scala hljs" data-lang="scala">val textRDD = sc.textFile(...)
val splitRDD = textRDD.flatMap(_.split(" "))
val tupleRDD = splitRDD.map((_, 1))
val reduceRDD = tupleRDD.reduceByKey(_ + _)</code></pre>
</div>
</div>
<div class="paragraph">
<p>用图形表示如下</p>
</div>
<div class="imageblock">
<div class="content">
<img src="https://doc-1256053707.cos.ap-beijing.myqcloud.com/20190619205906.png" alt="20190619205906" width="800">
</div>
</div>
<div class="paragraph">
<p>同样, <code>DStream</code> 的代码大致如下</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlightjs highlight"><code class="language-scala hljs" data-lang="scala">val lines: DStream[String] = ssc.socketTextStream(...)
val words: DStream[String] = lines.flatMap(_.split(" "))
val wordCounts: DStream[(String, Int)] = words.map(x =&gt; (x, 1)).reduceByKey(_ + _)</code></pre>
</div>
</div>
<div class="paragraph">
<p>同理, <code>DStream</code> 也可以形成 <code>DAG</code> 如下</p>
</div>
<div class="imageblock">
<div class="content">
<img src="https://doc-1256053707.cos.ap-beijing.myqcloud.com/20190619210315.png" alt="20190619210315" width="800">
</div>
</div>
<div class="paragraph">
<p>看起来 <code>DStream</code> 和 <code>RDD</code> 好像哟, 确实如此</p>
</div>
</div>
</div>
</dd>
<dt class="hdlist1"><code>RDD</code> 和 <code>DStream</code> 的区别</dt>
<dd>
<div class="exampleblock">
<div class="content">
<div class="imageblock">
<div class="content">
<img src="https://doc-1256053707.cos.ap-beijing.myqcloud.com/20190619212508.png" alt="20190619212508" width="800">
</div>
</div>
<div class="ulist">
<ul>
<li>
<p><code>DStream</code> 的数据是不断进入的, <code>RDD</code> 是针对一个数据的操作</p>
</li>
<li>
<p>像 <code>RDD</code> 一样, <code>DStream</code> 也有不同的子类, 通过不同的算子生成</p>
</li>
<li>
<p>一个 <code>DStream</code> 代表一个数据集, 其中包含了针对于上一个数据的操作</p>
</li>
<li>
<p><code>DStream</code> 根据时间切片, 划分为多个 <code>RDD</code>, 针对 <code>DStream</code> 的计算函数, 会作用于每一个 <code>DStream</code> 中的 <code>RDD</code></p>
</li>
</ul>
</div>
</div>
</div>
</dd>
<dt class="hdlist1"><code>DStream</code> 如何形式 <code>DAG</code></dt>
<dd>
<div class="exampleblock">
<div class="content">
<div class="imageblock">
<div class="content">
<img src="https://doc-1256053707.cos.ap-beijing.myqcloud.com/20190619212508.png" alt="20190619212508" width="800">
</div>
</div>
<div class="ulist">
<ul>
<li>
<p>每个 <code>DStream</code> 都有一个关联的 <code>DStreamGraph</code> 对象</p>
</li>
<li>
<p><code>DStreamGraph</code> 负责表示 <code>DStream</code> 之间的的依赖关系和运行步骤</p>
</li>
<li>
<p><code>DStreamGraph</code> 中会单独记录 <code>InputDStream</code> 和 <code>OutputDStream</code></p>
</li>
</ul>
</div>
</div>
</div>
</dd>
</dl>
</div>
</div>
</div>
</dd>
<dt class="hdlist1">切分流, 生成小批量</dt>
<dd>
<div class="sidebarblock">
<div class="content">
<div class="dlist">
<dl>
<dt class="hdlist1">静态和动态</dt>
<dd>
<div class="exampleblock">
<div class="content">
<div class="paragraph">
<p>根据前面的学习, 可以总结一下规律</p>
</div>
<div class="ulist">
<ul>
<li>
<p><code>DStream</code> 对应 <code>RDD</code></p>
</li>
<li>
<p><code>DStreamGraph</code> 表示 <code>DStream</code> 之间的依赖关系和运行流程, 相当于 <code>RDD</code> 通过 <code>DAGScheduler</code> 所生成的 <code>RDD DAG</code></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>但是回顾前面的内容, <code>RDD</code> 的运行分为逻辑计划和物理计划</p>
</div>
<div class="ulist">
<ul>
<li>
<p>逻辑计划就是 <code>RDD</code> 之间依赖关系所构成的一张有向无环图</p>
</li>
<li>
<p>后根据这张 <code>DAG</code> 生成对应的 <code>TaskSet</code> 调度到集群中运行, 如下</p>
</li>
</ul>
</div>
<div class="imageblock">
<div class="content">
<img src="https://doc-1256053707.cos.ap-beijing.myqcloud.com/20190619215422.png" alt="20190619215422" width="600">
</div>
</div>
<div class="paragraph">
<p>但是在 <code>DStream</code> 中则不能这么简单的划分, 因为 <code>DStream</code> 中有一个非常重要的逻辑, 需要按照时间片划分小批量</p>
</div>
<div class="ulist">
<ul>
<li>
<p>在 <code>Streaming</code> 中, <code>DStream</code> 类似 <code>RDD</code>, 生成的是静态的数据处理过程, 例如一个 <code>DStream</code> 中的数据经过 <code>map</code> 转为其它模样</p>
</li>
<li>
<p>在 <code>Streaming</code> 中, <code>DStreamGraph</code> 类似 <code>DAG</code>, 保存了这种数据处理的过程</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>上述两点, 其实描述的是静态的一张 <code>DAG</code>, 数据处理过程, 但是 <code>Streaming</code> 是动态的, 数据是源源不断的来的</p>
</div>
<div class="imageblock">
<div class="content">
<img src="https://doc-1256053707.cos.ap-beijing.myqcloud.com/20190619202422.png" alt="20190619202422" width="800">
</div>
</div>
<div class="paragraph">
<p>所以, 在 <code>DStream</code> 中, 静态和动态是两个概念, 有不同的流程</p>
</div>
<div class="imageblock">
<div class="content">
<img src="https://doc-1256053707.cos.ap-beijing.myqcloud.com/20190619212508.png" alt="20190619212508" width="800">
</div>
</div>
<div class="ulist">
<ul>
<li>
<p><code>DStreamGraph</code> 将 <code>DStream</code> 联合起来, 生成 <code>DStream</code> 之间的 <code>DAG</code>, 这些 <code>DStream</code> 之间的关系是相互依赖的关系, 例如一个 <code>DStream</code> 经过 <code>map</code> 转为另外一个 <code>DStream</code></p>
</li>
<li>
<p>但是把视角移动到 <code>DStream</code> 中来看, <code>DStream</code> 代表了源源不断的 <code>RDD</code> 的生成和处理, 按照时间切片, 所以一个 <code>DStream DAG</code> 又对应了随着时间的推进所产生的无限个 <code>RDD DAG</code></p>
</li>
</ul>
</div>
</div>
</div>
</dd>
<dt class="hdlist1">动态生成 <code>RDD DAG</code> 的过程</dt>
<dd>
<div class="exampleblock">
<div class="content">
<div class="paragraph">
<p><code>RDD DAG</code> 的生成是按照时间来切片的, <code>Streaming</code> 会维护一个 <code>Timer</code>, 固定的时间到达后通过如下五个步骤生成一个 <code>RDD DAG</code> 后调度执行</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>通知 <code>Receiver</code> 将收到的数据暂存, 并汇报存储的元信息, 例如存在哪, 存了什么</p>
</li>
<li>
<p>通过 <code>DStreamGraph</code> 复制出一套新的 <code>RDD DAG</code></p>
</li>
<li>
<p>将数据暂存的元信息和 <code>RDD DAG</code> 一同交由 <code>JobScheduler</code> 去调度执行</p>
</li>
<li>
<p>提交结束后, 对系统当前的状态 <code>Checkpoint</code></p>
</li>
</ol>
</div>
</div>
</div>
</dd>
</dl>
</div>
</div>
</div>
</dd>
<dt class="hdlist1">数据的产生和导入</dt>
<dd>
<div class="sidebarblock">
<div class="content">
<div class="dlist">
<dl>
<dt class="hdlist1"><code>Receiver</code></dt>
<dd>
<div class="exampleblock">
<div class="content">
<div class="paragraph">
<p>在 <code>Spark Streaming</code> 中一个非常大的挑战是, 很多外部的队列和存储系统都是分块的,  <code>RDD</code> 是分区的, 在读取外部数据源的时候, 会用不同的分区对照外部系统的分片, 例如</p>
</div>
<div class="imageblock">
<div class="content">
<img src="https://doc-1256053707.cos.ap-beijing.myqcloud.com/f738dbe3df690bc0ba8f580a3e2d1112.png" alt="f738dbe3df690bc0ba8f580a3e2d1112" width="800">
</div>
</div>
<div class="paragraph">
<p>不仅 <code>RDD</code>, <code>DStream</code> 中也面临这种挑战</p>
</div>
<div class="imageblock">
<div class="content">
<img src="https://doc-1256053707.cos.ap-beijing.myqcloud.com/20190619223634.png" alt="20190619223634" width="800">
</div>
</div>
<div class="paragraph">
<p>那么此处就有一个小问题</p>
</div>
<div class="ulist">
<ul>
<li>
<p><code>DStream</code> 中是 <code>RDD</code> 流, 只是 <code>RDD</code> 的分区对应了 <code>Kafka</code> 的分区就可以了吗?</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>答案是不行, 因为需要一套单独的机制来保证并行的读取外部数据源, 这套机制叫做 <code>Receiver</code></p>
</div>
</div>
</div>
</dd>
<dt class="hdlist1"><code>Receiver</code> 的结构</dt>
<dd>
<div class="exampleblock">
<div class="content">
<div class="imageblock">
<div class="content">
<img src="https://doc-1256053707.cos.ap-beijing.myqcloud.com/20190619224138.png" alt="20190619224138" width="800">
</div>
</div>
<div class="paragraph">
<p>为了保证并行获取数据, 对应每一个外部数据源的分区, 所以 <code>Receiver</code> 也要是分布式的, 主要分为三个部分</p>
</div>
<div class="ulist">
<ul>
<li>
<p><code>Receiver</code> 是一个对象, 是可以有用户自定义的获取逻辑对象, 表示了如何获取数据</p>
</li>
<li>
<p><code>Receiver Tracker</code> 是 <code>Receiver</code> 的协调和调度者, 其运行在 <code>Driver</code> 上</p>
</li>
<li>
<p><code>Receiver Supervisor</code> 被 <code>Receiver Tracker</code> 调度到不同的几点上分布式运行, 其会拿到用户自定义的 <code>Receiver</code> 对象, 使用这个对象来获取外部数据</p>
</li>
</ul>
</div>
</div>
</div>
</dd>
<dt class="hdlist1"><code>Receiver</code> 的执行过程</dt>
<dd>
<div class="exampleblock">
<div class="content">
<div class="imageblock">
<div class="content">
<img src="https://doc-1256053707.cos.ap-beijing.myqcloud.com/20190619224025.png" alt="20190619224025" width="800">
</div>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>在 <code>Spark Streaming</code> 程序开启时候, <code>Receiver Tracker</code> 使用 <code>JobScheduler</code> 分发 <code>Job</code> 到不同的节点, 每个 <code>Job</code> 包含一个 <code>Task</code> , 这个 <code>Task</code> 就是 <code>Receiver Supervisor</code>, 这个部分的源码还挺精彩的, 其实是复用了通用的调度逻辑</p>
</li>
<li>
<p><code>ReceiverSupervisor</code> 启动后运行 <code>Receiver</code> 实例</p>
</li>
<li>
<p><code>Receiver</code> 启动后, 就将持续不断地接收外界数据, 并持续交给 <code>ReceiverSupervisor</code> 进行数据存储</p>
</li>
<li>
<p><code>ReceiverSupervisor</code> 持续不断地接收到 <code>Receiver</code> 转来的数据, 并通过 <code>BlockManager</code> 来存储数据</p>
</li>
<li>
<p>获取的数据存储完成后发送元数据给 <code>Driver</code> 端的 <code>ReceiverTracker</code>, 包含数据块的 <code>id</code>, 位置, 数量, 大小 等信息</p>
</li>
</ol>
</div>
</div>
</div>
</dd>
</dl>
</div>
</div>
</div>
</dd>
<dt class="hdlist1">容错</dt>
<dd>
<div class="sidebarblock">
<div class="content">
<div class="paragraph">
<p>因为要非常长时间的运行, 对于任何一个流计算系统来说, 容错都是非常致命也非常重要的一环, 在 <code>Spark Streaming</code> 中, 大致提供了如下的容错手段</p>
</div>
<div class="dlist">
<dl>
<dt class="hdlist1">热备</dt>
<dd>
<div class="exampleblock">
<div class="content">
<div class="paragraph">
<p>还记得这行代码吗</p>
</div>
<div class="imageblock">
<div class="content">
<img src="https://doc-1256053707.cos.ap-beijing.myqcloud.com/20190619225306.png" alt="20190619225306" width="600">
</div>
</div>
<div class="paragraph">
<p>这行代码中的 <code>StorageLevel.MEMORY_AND_DISK_SER</code> 的作用是什么? 其实就是热备份</p>
</div>
<div class="ulist">
<ul>
<li>
<p>当 Receiver 获取到数据要存储的时候, 是交给 BlockManager 存储的</p>
</li>
<li>
<p>如果设置了 <code>StorageLevel.MEMORY_AND_DISK_SER</code>, 则意味着 <code>BlockManager</code> 不仅会在本机存储, 也会发往其它的主机进行存储, 本质就是冗余备份</p>
</li>
<li>
<p>如果某一个计算失败了, 通过冗余的备份, 再次进行计算即可</p>
</li>
</ul>
</div>
<div class="imageblock">
<div class="content">
<img src="https://doc-1256053707.cos.ap-beijing.myqcloud.com/20190619225830.png" alt="20190619225830" width="800">
</div>
</div>
<div class="paragraph">
<p>这是默认的容错手段</p>
</div>
</div>
</div>
</dd>
<dt class="hdlist1">冷备</dt>
<dd>
<div class="exampleblock">
<div class="content">
<div class="paragraph">
<p>冷备在 <code>Spark Streaming</code> 中的手段叫做 <code>WAL</code> (预写日志)</p>
</div>
<div class="ulist">
<ul>
<li>
<p>当 <code>Receiver</code> 获取到数据后, 会交给 <code>BlockManager</code> 存储</p>
</li>
<li>
<p>在存储之前先写到 <code>WAL</code> 中, <code>WAL</code> 中保存了 <code>Redo Log</code>, 其实就是记录了数据怎么产生的, 以便于恢复的时候通过 <code>Log</code> 恢复</p>
</li>
<li>
<p>当出错的时候, 通过 <code>Redo Log</code> 去重放数据</p>
</li>
</ul>
</div>
</div>
</div>
</dd>
<dt class="hdlist1">重放</dt>
<dd>
<div class="exampleblock">
<div class="content">
<div class="ulist">
<ul>
<li>
<p>有一些上游的外部系统是支持重放的, 比如说 <code>Kafka</code></p>
</li>
<li>
<p><code>Kafka</code> 可以根据 <code>Offset</code> 来获取数据</p>
</li>
<li>
<p>当 <code>SparkStreaming</code> 处理过程中出错了, 只需要通过 <code>Kafka</code> 再次读取即可</p>
</li>
</ul>
</div>
</div>
</div>
</dd>
</dl>
</div>
</div>
</div>
</dd>
</dl>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_3_操作">3. 操作</h2>
<div class="sectionbody">
<div class="exampleblock">
<div class="title">导读</div>
<div class="content">
<div class="paragraph">
<p>这一小节主要目的是为了了解 <code>Spark Streaming</code> 一些特别特殊和重要的操作, 一些基本操作基本类似 <code>RDD</code></p>
</div>
</div>
</div>
<div class="dlist">
<dl>
<dt class="hdlist1"><code>updateStateByKey</code></dt>
<dd>
<div class="sidebarblock">
<div class="content">
<div class="admonitionblock note">
<table>
<tr>
<td class="icon">
<i class="fa icon-note" title="Note"></i>
</td>
<td class="content">
需求: 统计整个流中, 所有出现的单词数量, 而不是一个批中的数量
</td>
</tr>
</table>
</div>
<div class="dlist">
<dl>
<dt class="hdlist1">状态</dt>
<dd>
<div class="exampleblock">
<div class="content">
<div class="ulist">
<ul>
<li>
<p>统计总数</p>
<div class="openblock">
<div class="content">
<div class="paragraph">
<p>入门案例中, 只能统计某个时间段内的单词数量, 因为 <code>reduceByKey</code> 只能作用于某一个 <code>RDD</code>, 不能作用于整个流</p>
</div>
<div class="paragraph">
<p>如果想要求单词总数该怎么办?</p>
</div>
</div>
</div>
</li>
<li>
<p>状态</p>
<div class="openblock">
<div class="content">
<div class="paragraph">
<p>可以使用状态来记录中间结果, 从而每次来一批数据, 计算后和中间状态求和, 于是就完成了总数的统计</p>
</div>
<div class="imageblock">
<div class="content">
<img src="https://doc-1256053707.cos.ap-beijing.myqcloud.com/20190620014145.png" alt="20190620014145" width="800">
</div>
</div>
</div>
</div>
</li>
</ul>
</div>
</div>
</div>
</dd>
<dt class="hdlist1">实现</dt>
<dd>
<div class="exampleblock">
<div class="content">
<div class="ulist">
<ul>
<li>
<p>使用 <code>updateStateByKey</code> 可以做到这件事</p>
</li>
<li>
<p><code>updateStateByKey</code> 会将中间状态存入 <code>CheckPoint</code> 中</p>
</li>
</ul>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlightjs highlight"><code class="language-scala hljs" data-lang="scala">val sparkConf = new SparkConf().setAppName("NetworkWordCount").setMaster("local[6]")
val sc = new SparkContext(sparkConf)
sc.setLogLevel("ERROR")
val ssc = new StreamingContext(sc, Seconds(1))

val lines: DStream[String] = ssc.socketTextStream(
  hostname = "localhost",
  port = "9999".toInt,
  storageLevel = StorageLevel.MEMORY_AND_DISK_SER)

val words = lines.flatMap(_.split(" ")).map(x =&gt; (x, 1))

// 使用 updateStateByKey 必须设置 Checkpoint 目录
ssc.checkpoint("checkpoint")

// updateStateByKey 的函数
def updateFunc(newValue: Seq[Int], runningValue: Option[Int]) = {
  // newValue 之所以是一个 Seq, 是因为它是某一个 Batch 的某个 Key 的全部 Value
  val currentBatchSum = newValue.sum
  val state = runningValue.getOrElse(0)
  // 返回的这个 Some(count) 会再次进入 Checkpoint 中当作状态存储
  Some(currentBatchSum + state)
}

// 调用
val wordCounts = words.updateStateByKey[Int](updateFunc)

wordCounts.print()

ssc.start()
ssc.awaitTermination()</code></pre>
</div>
</div>
</div>
</div>
</dd>
</dl>
</div>
</div>
</div>
</dd>
<dt class="hdlist1"><code>window</code> 操作</dt>
<dd>
<div class="sidebarblock">
<div class="content">
<div class="admonitionblock note">
<table>
<tr>
<td class="icon">
<i class="fa icon-note" title="Note"></i>
</td>
<td class="content">
需求: 计算过 <code>30s</code> 的单词总数, 每 <code>10s</code> 更新一次
</td>
</tr>
</table>
</div>
<div class="dlist">
<dl>
<dt class="hdlist1">实现</dt>
<dd>
<div class="exampleblock">
<div class="content">
<div class="ulist">
<ul>
<li>
<p>使用 <code>window</code> 即可实现按照窗口组织 RDD</p>
</li>
</ul>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlightjs highlight"><code class="language-scala hljs" data-lang="scala">val sparkConf = new SparkConf().setAppName("NetworkWordCount").setMaster("local[6]")
val sc = new SparkContext(sparkConf)
sc.setLogLevel("ERROR")
val ssc = new StreamingContext(sc, Seconds(1))

val lines: DStream[String] = ssc.socketTextStream(
  hostname = "localhost",
  port = 9999,
  storageLevel = StorageLevel.MEMORY_AND_DISK_SER)

val words = lines.flatMap(_.split(" ")).map(x =&gt; (x, 1))

// 通过 window 操作, 会将流分为多个窗口
val wordsWindow = words.window(Seconds(30), Seconds(10))
// 此时是针对于窗口求聚合
val wordCounts = wordsWindow.reduceByKey((newValue, runningValue) =&gt; newValue + runningValue)

wordCounts.print()

ssc.start()
ssc.awaitTermination()</code></pre>
</div>
</div>
<div class="ulist">
<ul>
<li>
<p>既然 <code>window</code> 操作经常配合 <code>reduce</code> 这种聚合, 所以 <code>Spark Streaming</code> 提供了较为方便的方法</p>
</li>
</ul>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlightjs highlight"><code class="language-scala hljs" data-lang="scala">val sparkConf = new SparkConf().setAppName("NetworkWordCount").setMaster("local[6]")
val sc = new SparkContext(sparkConf)
sc.setLogLevel("ERROR")
val ssc = new StreamingContext(sc, Seconds(1))

val lines: DStream[String] = ssc.socketTextStream(
  hostname = "localhost",
  port = 9999,
  storageLevel = StorageLevel.MEMORY_AND_DISK_SER)

val words = lines.flatMap(_.split(" ")).map(x =&gt; (x, 1))

// 开启窗口并自动进行 reduceByKey 的聚合
val wordCounts = words.reduceByKeyAndWindow(
  reduceFunc = (n, r) =&gt; n + r,
  windowDuration = Seconds(30),
  slideDuration = Seconds(10))

wordCounts.print()

ssc.start()
ssc.awaitTermination()</code></pre>
</div>
</div>
</div>
</div>
</dd>
<dt class="hdlist1">窗口时间</dt>
<dd>
<div class="exampleblock">
<div class="content">
<div class="imageblock">
<div class="content">
<img src="https://doc-1256053707.cos.ap-beijing.myqcloud.com/20190620021454.png" alt="20190620021454" width="600">
</div>
</div>
<div class="ulist">
<ul>
<li>
<p>在 <code>window</code> 函数中, 接收两个参数</p>
<div class="openblock">
<div class="content">
<div class="ulist">
<ul>
<li>
<p><code>windowDuration</code> 窗口长度, <code>window</code> 函数会将多个 <code>DStream</code> 中的 <code>RDD</code> 按照时间合并为一个, 那么窗口长度配置的就是将多长时间内的 <code>RDD</code> 合并为一个</p>
</li>
<li>
<p><code>slideDuration</code> 滑动间隔, 比较好理解的情况是直接按照某个时间来均匀的划分为多个 <code>window</code>, 但是往往需求可能是统计最近 <code>xx分</code> 内的所有数据, 一秒刷新一次, 那么就需要设置滑动窗口的时间间隔了, 每隔多久生成一个 <code>window</code></p>
</li>
</ul>
</div>
</div>
</div>
</li>
<li>
<p>滑动时间的问题</p>
<div class="openblock">
<div class="content">
<div class="ulist">
<ul>
<li>
<p>如果 <code>windowDuration &gt; slideDuration</code>, 则在每一个不同的窗口中, 可能计算了重复的数据</p>
</li>
<li>
<p>如果 <code>windowDuration &lt; slideDuration</code>, 则在每一个不同的窗口之间, 有一些数据为能计算进去</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>但是其实无论谁比谁大, 都不能算错, 例如, 我的需求有可能就是统计一小时内的数据, 一天刷新两次</p>
</div>
</div>
</div>
</li>
</ul>
</div>
</div>
</div>
</dd>
</dl>
</div>
</div>
</div>
</dd>
</dl>
</div>
</div>
</div>
        </div>
      </div>
    </body>
  </html>
